from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage
from page_object.login_page import LoginPage


class AssetPage(BasePage):
    url = r'/bsams/front/asset/asset_list.do/'
    # 新增定位
    add_btn_loc = (By.CLASS_NAME, 'button_true')
    add_title_loc = (By.ID, 'title')
    add_providerId_loc = (By.ID, 'providerId')
    add_storageId_loc = (By.ID, 'storageId')
    add_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')
    # 修改定位
    mod_btn_loc = (By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[3]/table/tbody/tr[2]/td[8]/a[1]')
    mod_title_loc = (By.ID, 'title')
    mod_storageId_loc = (By.ID, 'storageId')
    mod_providerId_loc = (By.ID, 'providerId')
    mod_ok_loc = (By.CLASS_NAME, 'button_fujian.margin')
    # 查询定位
    query_btn_loc = (By.ID, 'status')
    query_title_loc = (By.ID, 'title')
    query_ok_loc = (By.CLASS_NAME, 'search_button')
    # 借用定位
    lend_btn_loc = (By.LINK_TEXT, '借用')
    lend_departId_loc = (By.ID, 'departId')
    lend_remark_loc = (By.ID, 'remark')
    lend_ok_loc = (By.CLASS_NAME, 'button_fujian margin')
    # 归还定位
    return_btn_loc = (By.LINK_TEXT, '归还')
    return_ok_loc = (By.CLASS_NAME, 'button_fujian margin')
    
    #新增方法
    def asset_add(self, title, providerId, storageId):
        self.open()
        self.ele_click(self.add_btn_loc)
        self.ele_input(self.add_title_loc, title)
        self.ele_select(self.add_providerId_loc, providerId)
        self.ele_select(self.add_storageId_loc, storageId)
        self.ele_click(self.add_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_mod(self, title, providerId, storageId):
        self.url = r'/bsams/front/asset/asset_list.do'
        self.open()
        self.ele_click(self.mod_btn_loc)
        self.ele_clear(self.add_title_loc)
        self.ele_input(self.mod_title_loc, title)
        self.ele_select(self.mod_providerId_loc, providerId)
        self.ele_select(self.mod_storageId_loc, storageId)
        self.ele_click(self.mod_ok_loc)
        sleep(3)
        text = self.get_alert_text()
        self.alert_click()
        return text

    def asset_query(self, status, title):
        self.open()
        self.ele_select(self.query_btn_loc, status)
        self.ele_clear(self.query_title_loc)
        self.ele_input(self.query_title_loc, title)
        self.ele_click(self.query_ok_loc)
        sleep(2)

    def asset_lend(self, departId, remark):
        self.open()
        sleep(2)
        self.ele_click(self.lend_btn_loc)
        self.ele_select(self.lend_btn_loc, departId)
        self.ele_clear(self.lend_remark_loc)
        self.ele_input(self.lend_remark_loc, remark)
        self.ele_click(self.lend_ok_loc)
        sleep(2)

    def asset_return(self, departId='软件部', remark='1'):
        self.open()
        sleep(2)
        self.asset_lend(departId, remark)
        self.ele_click(self.return_btn_loc)
        self.ele_click(self.return_ok_loc)
        sleep(2)

    if __name__ == '__main__':
        title = '戴尔电脑'
        providerId = '维信科技发展有限公司'
        storageId = '电脑设备库'
        status = '未使用'
        departId = '软件部'
        remark = '1'

        chrome_location = r'D:\RJCS\谷歌浏览器内网通紫域和驱动安装\chrome-win\chrome.exe'
        options = webdriver.ChromeOptions()
        options.binary_location = chrome_location

        driver = webdriver.Chrome(options=options)

        login_page = LoginPage(driver)
        login_page.login()

        assert_page = AssetPage(driver)
        # text = assert_page.asset_add(title, providerId, storageId)
        # text = assert_page.asset_mod(title, providerId, storageId)
        # print(text)
        # assert_page.asset_query(status,title)
        assert_page.asset_lend(departId, remark)
        # assert_page.asset_return()
        driver.quit()
